﻿using System;
using System.Windows.Input;

namespace SilverAmp
{
    public class DelegateCommand<T> : ICommand
    {
        public DelegateCommand(Action<T> executeAction,
                               Func<T, bool> canExecuteAction)
        {
            this.executeAction = executeAction;
            this.canExecuteAction = canExecuteAction;
        }
        public DelegateCommand(Action<T> executeAction)
            : this(executeAction, null)
        {
        }
        public bool CanExecute(object parameter)
        {
            if (canExecuteAction == null) return true;
            return (canExecuteAction((T)parameter));
        }
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (executeAction!=null)
            executeAction((T)parameter);
        }

        readonly Action<T> executeAction;
        readonly Func<T, bool> canExecuteAction;
    }
}